package onebot

import (
	"fmt"
	"os"
	"runtime"
	"strings"

	"gitee.com/GopherX/mqsdk/api"
)

func INFO(s string, v ...interface{}) {
	api.A输出日志("[INFO] " + fmt.Sprintf(s, v...))
}
func WARN(s string, v ...interface{}) {
	api.A输出日志("[WARN] " + fmt.Sprintf(s, v...))
}
func DEBUG(s string, v ...interface{}) {
	api.A输出日志("[DEBUG] " + fmt.Sprintf(s, v...))
}
func ERROR(s string, v ...interface{}) {
	api.A输出日志("[ERROR] " + fmt.Sprintf(s, v...))
}
func META(s string, v ...interface{}) {
	api.A输出日志("[META] " + fmt.Sprintf(s, v...))
}
func TEST(s string, v ...interface{}) {
	api.A输出日志("[TEST] " + fmt.Sprintf(s, v...))
}
func PathExecute() string {
	dir, err := os.Getwd()
	if err != nil {
		ERROR("判断当前运行路径失败")
	}
	return dir + "/"
}
func CreatePath(path string) {
	if !PathExists(path) {
		length := len(path)
		switch {
		case path[length-1:] != "/":
			path = path[:strings.LastIndex(path, "/")]
		case path[length-1:] != "\\":
			path = path[:strings.LastIndex(path, "\\")]
		default:
			//
		}
		err := os.MkdirAll(path, 0644)
		if err != nil {
			ERROR("生成应用目录失败")
		}
	}
}
func PathExists(path string) bool {
	_, err := os.Stat(path)
	return err == nil || os.IsExist(err)
}
func ProtectRun(entry func() int, label string) int {
	return func() int {
		res := entry()
		defer func() {
			err := recover()
			if err != nil {
				ERROR("[协程] %v协程发生了不可预知的错误,可向【Gopher X】提交issue：%v",
					label, err)
				buf := make([]byte, 1<<16)
				runtime.Stack(buf, true)
				ERROR("traceback:\n%v", string(buf))
			}
		}()
		return res
	}()

}
